Skip to content

Fetch the Telegram pictures on screen first, ahead of a channel left and of what is off screen. - #263

Closed
abalakh wants to merge 2 commits into
TinyComms:mainfrom
abalakh:telegram-visible-image-priority
Closed

abalakh wants to merge 2 commits into
TinyComms:mainfrom
abalakh:telegram-visible-image-priority

Conversation

@abalakh

@abalakh abalakh commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Depends on #262. This branch is #262's commit plus one. Review the last commit. Once #262 is squash-merged, this is rebased onto main and the diff shrinks to that commit.

What changed and why

A Telegram chat opened while another channel's thumbnails are still downloading now shows its own pictures first. Before, every thumbnail a page named waited in one line, one file at a time, 250 ms apart. The pictures on screen sat behind the off-screen ones and behind every thumbnail of the channel just left, for the 15 to 20 seconds reported. With this and #262 on a real account, a new channel's preview shows at once, its thumbnail in about a second, and the sharp picture a second later.

The Telegram file worker now takes three lines in order:

  1. the original the viewer is waiting on;
  2. the pictures messages show that the shell asked for because they are on screen, the latest asked first;
  3. everything else, in the order pages named it.

The shell already asks for what it draws (Request::Fetch). chatd already hands an id the media cache does not know, or (since #262) a Telegram file that left the disk, to the adapter as Command::Wanted. Telegram used that only for the viewer's originals. Nothing in the shell or the protocol changes.

Changes

  • Telegram files (files.rs):
    • Files keeps the place of each picture a message shows (a photo's thumbnail, a video's, GIF's or sticker's still), next to the originals it already kept. When the shell asks for one, it goes ahead of everything not on screen.
    • An id the shell asks for before any page named it here is remembered for 30 s. This happens when the page was on screen from the store while the conversation was read again. The picture goes ahead when the page names it.
    • A file waiting in line keeps one place. Named again, it takes the newer page's reference; asked for again, it moves. Three asks for one thumbnail are one download.
    • The line never holds more than the 256 it held before. Asks are never refused at the door, so the worker trims after each job: first the last file a page named, then the picture asked for earliest. The viewer's original is never trimmed.
    • What is remembered forgets the oldest half instead of everything at once. That covers places (now 8,192, about two per photo, so as many photos as before) and early asks (512).
    • Avatars and custom emoji keep their turn, and so does a picker's tile (Files::want_tiles, used by catalog.rs). The sidebar, people list and pickers ask for those by the hundred, whether in sight or not. Chat-list avatars still wait for their rows, and go back ahead of the rest when their rows come back.
    • What is being fetched is never cut short, and a flood wait still holds the whole worker until it has passed.
  • Show the preview Telegram sends inside a photo until its thumbnail lands, and stop failing photos the cache removed #262's evicted thumbnails: a thumbnail the cache removed is now fetched again as soon as the shell asks for it on screen, with no page read again. Show the preview Telegram sends inside a photo until its thumbnail lands, and stop failing photos the cache removed #262 left that for this change. Its test now asserts it, and docs/architecture.md, blobs.rs and Command::Wanted say so.
  • Fake wire:
    • photo-pages N writes N photo posts into Field Notes and one photo into Trail crew, where the next page read finds them, with no update. The group's thumbnail is a real JPEG (fake_crew_m.jpg, magenta across a green band).
    • flood-file N refuses the next upload.GetFile with a flood wait.

Type

  • feature
  • fix
  • perf
  • refactor
  • chore
  • docs

Platforms

  • windows
  • macos
  • linux
  • android
  • ios

Proof

Scenario: e2e/scenarios/telegram-visible-image-priority

Lab run: pending.

The real window opens Field Notes with 30 uncached photo posts. It then switches with Ctrl+K, "Trail crew", Enter while most of the channel's thumbnails wait.

  • From the fake wire's telegram.calls: the group's thumbnail must be asked for before channel thumbnails that were already waiting when the group opened.
  • From screen pixels: the group's photo must be drawn (magenta above and below a green band).

The base build's fake wire does not know photo-pages, so the base run stops at "Field Notes opens, and Telegram is asked for its 30 photos' thumbnails one at a time". A base that had the posts would ask for the group's thumbnail after every channel thumbnail and fail the order step. scenario.json says why macOS and the phones are not recorded. test_drive.py covers the pixel check on drawn frames, including decoys.

Tests:

  • cargo test -p chatd --lib --tests: all pass.
  • New in tests/telegram.rs, through chatd's socket and the fake wire. The first four fail with Show the preview Telegram sends inside a photo until its thumbnail lands, and stop failing photos the cache removed #262's own files.rs:
    • the_pictures_on_screen_are_fetched_before_those_still_waiting_from_a_channel_left_or_off_screen: the picture asked for waits only for what was in flight.
    • a_picture_asked_for_before_its_page_is_read_goes_ahead_once_the_page_names_it
    • the_original_the_viewer_waits_on_goes_before_the_pictures_on_screen: a flood wait holds the worker while both are asked for, so the order is the line's.
    • Show the preview Telegram sends inside a photo until its thumbnail lands, and stop failing photos the cache removed #262's a_thumbnail_gone_from_disk_is_never_marked_failed_and_telegram_keeps_it_again, which now asserts the thumbnail is fetched again once, without a page read.
    • a_flood_wait_on_a_picture_on_screen_holds_every_file_until_it_has_passed, which guards behaviour that already held.
  • Unit tests in files.rs: the line's order, one place per file with the newest reference, the trim order, the half-forgetting memory, early asks only while recent, and a picker's tiles keeping their turn.
  • A release build of this branch ran for a while on a real account on macOS.

Compatibility

No protocol, store or server change. An older shell sends the same Fetch it sends now.

Risk and rollback

Release note

Telegram photos on screen load first, instead of waiting behind the pictures of a channel you just left.

…nds, and stop failing photos the cache removed.

Telegram puts a tiny copy of every photo (its stripped thumbnail, about
40 px) inside the message. chatd now rebuilds it into a JPEG from
Telegram's fixed header and keeps it on disk as soon as the message is
read, without asking the network. It reaches the shell as the new
optional Attachment.preview. The tile draws it stretched over the
photo's place while the chat-sized thumbnail downloads, and the
thumbnail replaces it in the same place at the same size. Load images
and spoilers hide it exactly as they hide the photo. A photo with no
usable preview shows the image icon as before. Album tiles use the
same code.

What came inside a message is kept at once, on the thread that reads
the message, so a preview never waits behind the downloads it stands
in for. Each write has its own part file, so two writers of the same
file never cut each other short.

The media cache retried a Telegram file it had evicted at Telegram's
internal tg://file/ name, which no host serves, and marked it
"Telegram does not serve files from that address." The photo then said
it could not load, with a Retry that could not work, until Telegram
read the message again. The cache now leaves those files to Telegram
and never marks them failed, so the box shows the preview or the icon,
and chatd clears the old failures once at start. Signal, Messages and
web addresses are unchanged.

The e2e scenario telegram-photo-preview holds the thumbnail on the fake
wire, checks the screen shows the preview, lets the thumbnail go, and
checks it lands in the same place.
…and of what is off screen.

Telegram's files come one at a time, 250 ms apart, and every thumbnail a
page named waited in one line: a channel opened after an image-heavy one
waited behind all of the first one's thumbnails, and behind its own that
were not on screen, for 15 to 20 seconds.

The file worker now keeps three lines: the original the viewer waits on,
then the pictures messages show that the shell asked for because they
are on screen (Command::Wanted), the latest asked first, then the rest
in the order pages named them. Telegram keeps the place of each picture
a message shows, as it kept the originals', so an ask moves it ahead. An
ask for a picture no page has named yet is remembered for 30 s and goes
ahead when a page does. A file waiting keeps one place, with the newest
page's reference, so repeated asks are one download. The line still
holds no more than 256: asks are never refused, and the worker lets go
of the last-named background file first. Avatars, custom emoji and a
picker's tiles keep their turn. What is remembered forgets its oldest
half, never all at once.

With TinyComms#262, a thumbnail the cache removed is fetched again as soon as the
shell asks for it on screen; its test and the docs now say so.

The fake wire gains photo-pages (photo posts in Field Notes and a real
JPEG in Trail crew, read only with their pages) and flood-file. The e2e
scenario telegram-visible-image-priority opens Field Notes, switches to
Trail crew with Ctrl+K while the channel's thumbnails wait, checks from
the wire's log that the group's photo went first and from the screen
that it is drawn.
@abalakh

abalakh commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Pls note it depends on #262 - both features complement each other and have a bit of conflicting changes, not to mention developing them separately will require a follow-up PR to correctly wire them together when both are merged, so i decided to just make one dependent on the other.

Before these changes, i often saw empty pictures placeholders when switching channels for 15-20s which is beyond acceptable. With these 2 applied together, it's usually up to 1s for thumbnail and extra 1s to load full picture, which is still not perfect but huge improvement nonetheless, with them it actually becomes usable.

@itz4blitz

Copy link
Copy Markdown
Collaborator

Superseded by and merged in #903 (d387971 — Release 2026-11-20 combined PR).

@itz4blitz itz4blitz closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants